import game

class MiniMaxTree (game.TreeNode):
    
    def __init__(self, game, depth):
        
        if self.depth <= 0:

            self.score = game.score()
            self.best  = None
            self.child = None

        else:
        
            turn = game.turn()
            self.child = {}
            best = None
            maximum = 0
            for m in game.valid():
                self.child[m] = MiniMaxTree(game.move(m), depth - 1)
                if self.child[m].score[turn] > maximum:
                    maximum = self.child[m].score[turn]
                    best = m

            self.best = best
            self.score = self.child[best].score

    def get_child(self, move):
        
        if self.child != None:
            if move in self.child:
                return self.child[move]
            else:
                return None
        else:
            return None

    def choose(self):

        return self.best

    def get_score(self):
        
        if self.best is not None:
            self.score = self.child[self.best].get_score()

        return self.score
